package com.baymax.test.scene.demo

import android.os.AsyncTask
import android.os.Bundle
import android.os.HandlerThread
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.internal.schedulers.ExecutorScheduler
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.await
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       RxjavaScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/6/15 22:01
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class RxjavaScene : BaseScene() {

    val worker = Schedulers.io().createWorker()

    val task = Runnable {
        println("${Thread.currentThread().name} task start")
        Thread.sleep(1000)
        println("${Thread.currentThread().name} task end")
    }


    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {
            addButton("schedule io") {
                repeat(3) {
                    Schedulers.io().scheduleDirect(task)
                    println("add task")
                }
            }

            addButton("schedule compu") {
                repeat(3) {
                    Schedulers.computation().scheduleDirect(task)
                    println("add task")
                }
            }

            addButton("schedule main") {
                repeat(3) {
                    AndroidSchedulers.mainThread().scheduleDirect(task)
                    println("add task")
                }
            }
            addButton("schedule single") {
                repeat(3) {
                    Schedulers.single().scheduleDirect(task)
                    println("add task")
                }
            }


            val s1 = Schedulers.from(AsyncTask.SERIAL_EXECUTOR)
            val w1 = s1.createWorker()
            addButton("schedule s1") {
                repeat(3) {
                    s1.scheduleDirect(task)
                    println("add task")
                }
            }

            addButton("test set dispose") {

               val  d = Completable.create { e ->
                    val d1 =  Completable.timer(1000, TimeUnit.SECONDS).subscribe {
                        println("task1 complete")
                        e.onComplete()
                    }
                    e.setDisposable(d1)
                }.subscribe { println("task2 complete") }

//                launch {
//                    delay(800)
//                    println("task2 d $d")
//                    d?.dispose()
//                    println("task2 d $d")
//                }
            }

        }
    }
}